package com.eranga.project.control.data;

import java.util.ArrayList;

import com.eranga.project.model.Area;
import com.eranga.project.model.AreaCoordinates;
import com.eranga.project.model.Log;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class LogDS {
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	
	public LogDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	
	
	@SuppressWarnings("static-access")
	public int createStatus(Log log) {
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		
		ContentValues values = new ContentValues();
		values.put(dbHelper.LOG_AREA, log.getArea());
		values.put(dbHelper.LOG__TRACKED_MAX_SPEED, log.getSpeed());
		values.put(dbHelper.LOG_CLOSEST_LATITUDE, log.getLat());
		values.put(dbHelper.LOG_CLOSEST_LONGITUDE, log.getLon());	
		values.put(dbHelper.LOG_DATE_TIME, log.getDateTime());	
		values.put(dbHelper.LOG_DRIVER_ID, log.getDriver_id());	
		values.put(dbHelper.LOG_STATUS, log.getStatus());	
		values.put(dbHelper.ISSYNC, 0);	
		
		int id = (int) dB.insert(dbHelper.TABLE_LOG_DETAILS, null, values);
		dB.close();
		
		return id;
		
	}
	

	public ArrayList<Log> getAllUnSync() {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<Log> results = new ArrayList<Log>();
		
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_LOG_DETAILS+ " WHERE "+dbHelper.ISSYNC+"=0";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){
			
			Log log = new Log();
			
			log.setId(cursor.getInt(cursor.getColumnIndex(dbHelper.LOG_ID)));
			log.setArea(cursor.getString(cursor.getColumnIndex(dbHelper.LOG_AREA)));
			log.setSpeed(cursor.getDouble(cursor.getColumnIndex(dbHelper.LOG__TRACKED_MAX_SPEED)));
			log.setLat(cursor.getDouble(cursor.getColumnIndex(dbHelper.LOG_CLOSEST_LATITUDE)));
			log.setLon(cursor.getDouble(cursor.getColumnIndex(dbHelper.LOG_CLOSEST_LONGITUDE)));
			log.setDateTime(cursor.getString(cursor.getColumnIndex(dbHelper.LOG_DATE_TIME)));
			log.setDriver_id(cursor.getString(cursor.getColumnIndex(dbHelper.LOG_DRIVER_ID)));
			log.setStatus(cursor.getString(cursor.getColumnIndex(dbHelper.LOG_STATUS)));
			
			results.add(log);
		}
		dB.close();
		return results;
	}
	
	//updating sync success flag
	public int updateRCSProfileSyncStatus(int id) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}

	
		ContentValues values = new ContentValues();

		values.put(dbHelper.ISSYNC, "1");

		int updatedRowCount = dB.update(dbHelper.TABLE_LOG_DETAILS, values,dbHelper.LOG_ID + " = "+id,null);

		dB.close();

		return updatedRowCount;
	}
}
